package com.newer.service.sys.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.newer.dao.sys.mapper.RegionMapper;
import com.newer.pojo.sys.Region;
import com.newer.pojo.sys.RegionExample;
import com.newer.pojo.sys.RegionExample.Criteria;
import com.newer.service.sys.iter.RegionService;
import com.newer.utils.sys.BootstrapDataTable;
import com.newer.utils.sys.Conditions;
import com.newer.utils.sys.ConvertString;
import com.newer.utils.sys.IsNotNullUtil;
import com.newer.utils.sys.PinYin4jUtils;
import com.newer.utils.sys.RealExcel;

/**
 * 区域管理业务层
 * 
 * @author ShuFan
 */
@Service("regionService")
public class RegionServiceImpl implements RegionService {

	@Autowired
	private RegionMapper regionMapper;

	/**
	 * 查询所有区域信息
	 */
	@Override
	public BootstrapDataTable selectRegionByExample(BootstrapDataTable dataTable, Conditions conditions) {
		System.out.println("查询角色" + dataTable);
		// 给PageHelper设参数
		PageHelper.offsetPage(dataTable.getOffset(), dataTable.getLimit());
		// 实例化region条件对象
		RegionExample regionExample = new RegionExample();
		// 设置查询对象
		Criteria createCriteria = regionExample.createCriteria();
		// 设置And or 查询条件
		Criteria createCriteria2 = regionExample.createCriteria();

		Criteria createCriteria3 = regionExample.createCriteria();
		// 去重
		regionExample.setDistinct(true);
		// 设置查询条件
		if (IsNotNullUtil.isNotNull(conditions)) {

			if (IsNotNullUtil.isNotNull(conditions.getType()) && conditions.getType().equals("province")) {
				createCriteria.andProvinceLike("%" + conditions.getContent() + "%");
			}
			if (IsNotNullUtil.isNotNull(conditions.getType()) && conditions.getType().equals("city")) {
				createCriteria.andCityLike("%" + conditions.getContent() + "%");
			}
			if (IsNotNullUtil.isNotNull(conditions.getType()) && conditions.getType().equals("district")) {
				createCriteria.andDistrictLike("%" + conditions.getContent() + "%");
			}

			if (IsNotNullUtil.isNotNull(conditions.getType()) && conditions.getType().equals("all")) {
				Criteria i = createCriteria.andProvinceLike("%" + conditions.getContent() + "%");
				System.out.println("-------------------这是循环判断条件all进入的第一个条件判断" + i);
			}
			if (IsNotNullUtil.isNotNull(conditions.getType()) && conditions.getType().equals("all")) {
				Criteria i = createCriteria2.andCityLike("%" + conditions.getContent() + "%");
			}
			if (IsNotNullUtil.isNotNull(conditions.getType()) && conditions.getType().equals("all")) {
				Criteria i = createCriteria3.andDistrictLike("%" + conditions.getContent() + "%");
			}
		}

		// 设值
		regionExample.or(createCriteria2);
		regionExample.or(createCriteria3);
		// 设置排序字段与数据库字段不一致 判断排序字段是否为空
		if (IsNotNullUtil.isNotNull(dataTable)) {
			// 调用工具类
			String convertStr = ConvertString.convertStr(dataTable.getSort());
			dataTable.setSort(convertStr);
		}

		// 设置排序字段
		regionExample.setOrderByClause(dataTable.getSort() + " " + dataTable.getOrder());
		// 查询区域列表
		List<Region> regionList = regionMapper.selectByExample(regionExample);
		// 实例一个PageInfo对象 ，并设置参数regionList
		PageInfo<Region> pageInfo = new PageInfo<Region>(regionList);
		// 输出区域分页数据
		for (Region region : regionList) {
			System.out.println(region);
		}
		// 实例一个数据表对象
		BootstrapDataTable bootDataTable = new BootstrapDataTable();
		// 设置总记录数
		bootDataTable.setTotal((int) pageInfo.getTotal());
		// 设置每页显示数据
		bootDataTable.setRows(pageInfo.getList());
		// 返回数据
		return bootDataTable;
	}

	/**
	 * 根据id查询一条区域信息
	 */
	@Override
	public Region selectByPrimaryKey(String id) {
		// TODO Auto-generated method stub
		return regionMapper.selectByPrimaryKey(id);
	}

	/**
	 * 向区域表中插入一条区域信息
	 */
	@Override
	public int insertSelective(Region record) {
		// TODO Auto-generated method stub
		return regionMapper.insertSelective(record);
	}

	/**
	 * 根据区域id删除一条区域信息
	 */
	@Override
	public int deleteByPrimaryKey(String id) {
		// TODO Auto-generated method stub
		return regionMapper.deleteByPrimaryKey(id);
	}

	/**
	 * 根据主键修改区域信息
	 */
	@Override
	public int updateByPrimaryKeySelective(Region record) {
		// TODO Auto-generated method stub
		return regionMapper.updateByPrimaryKeySelective(record);
	}

	/**
	 * 批量删除区域信息
	 */
	@Override
	public int deleteRegionByIds(String ids[]) {
		return regionMapper.deleteRegionByIds(ids);
	}

	@Override
	public String insertRegions(CommonsMultipartFile fileName,HttpSession session) {
		// 创建处理EXCEL
				RealExcel readExcel = new RealExcel();
				// 解析excel，获取客户信息集合。
				List<Region> regionList = readExcel.getExcelInfo(fileName, session);
				for (Region region : regionList) {
					if (IsNotNullUtil.isNull(region.getId())) {
						return "错误:id不能为空";
					} 
					if (IsNotNullUtil.isNull(region.getProvince())) {
						return "错误:省份不能为空";
					}
					if (IsNotNullUtil.isNull(region.getCity())) {
						return "错误:城市不能为空";
					}
					if (IsNotNullUtil.isNull(region.getDistrict())) {
						return "错误:区域不能为空";
					} 
					if (IsNotNullUtil.isNull(region.getPostCode())) {
						return "错误:邮政编码不能为空";
					}else {
						// 防止联系电话不能重复
						List<Region> postCode = regionMapper.selectByPostCode(region.getPostCode());
						if (postCode.size() != 0) {
							return "错误:区域<" + region.getPostCode()+ ">已经备案";
						}
					}
				}
				// 迭代添加客户信息（注：实际上这里也可以直接将customerList集合作为参数，
				//在Mybatis的相应映射文件中使用foreach标签进行批量添加。）
				int i = regionMapper.importRegions(regionList);
				if (i!=0) {
					return "成功";
				}
				return "错误:导入失败";
	}
}